package com.lvyerose.hello.asymmetricgridviewtest.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lvyerose.hello.asymmetricgridviewtest.R;
import com.lvyerose.hello.asymmetricgridviewtest.mode.CustomItem;

import java.util.List;

/**
 * Created by Rose on 2015/6/29.
 * 传统Adapter适配器，注意数据源必须为实现AsymmetricItem接口的类的集合
 */
public class MeListViewAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<CustomItem> items;
    private Context mContext;


    public MeListViewAdapter(Context context, List<CustomItem> items) {
        mContext = context;
        this.items = items;
        layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        CustomItem item = items.get(position);
        boolean isRegular = getItemViewType(position) == 0;

        convertView = layoutInflater.inflate(
                isRegular ? R.layout.regular_item : R.layout.regular_not_item, null);

        TextView textView;
        if (isRegular) {
            textView = (TextView) convertView.findViewById(R.id.textview);
        } else {
            textView = (TextView) convertView.findViewById(R.id.textview_odd);
        }

        textView.setText(String.valueOf(item.getPosition()));

        return convertView;
    }

    /**
     * 根据不同位置加载不同布局
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? 1 : 0;
    }


}
